package io.renren.modules.quickService.controller;

import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;
import io.renren.modules.quickService.constants.AppointType;
import io.renren.modules.quickService.constants.AuditStatus;
import io.renren.modules.quickService.constants.OrderStatus;
import io.renren.modules.quickService.entity.OrderEntity;
import io.renren.modules.quickService.entity.OrderOperationEntity;
import io.renren.modules.quickService.service.AreacodeService;
import io.renren.modules.quickService.service.OrderOperationService;
import io.renren.modules.quickService.service.OrderService;
import io.renren.modules.sys.controller.AbstractController;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Date;
import java.util.Map;



/**
 * 订单表
 *
 * @author zenger380
 * @email zenger380@163.com
 * @date 2018-09-19 17:05:51
 */
@RestController
@RequestMapping("quickService/order")

public class OrderController extends AbstractController {
    @Autowired
    private OrderService orderService;
    @Autowired
    private AreacodeService areacodeService;
    @Autowired
    private OrderOperationService orderOperationService;
    /**
     * 列表
     */
    @PostMapping("/list")
    @RequiresPermissions("quickService:order:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = orderService.queryPage2(params);
        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @GetMapping("/info/{id}")
    @RequiresPermissions("quickService:order:info")
    public R info(@PathVariable("id") Long id){
        OrderEntity order = orderService.selectById(id);
        String[]  adminCodes =  new  String[3];
        if(order.getAdminCode() !=null){
            adminCodes =  areacodeService.getCascadeAreaByLastAdminCode(order.getAdminCode());
        }
        return R.ok().put("order", order).put("adminCodes",adminCodes);
    }

    /**
     * 保存
     */
    @PostMapping("/save")
    @RequiresPermissions("quickService:order:save")
    public R save(@RequestBody OrderEntity order){
        order.setStatus(OrderStatus.TO_RELEASE.getKey());
        order.setAuditStatus(AuditStatus.TO_AUDIT.getKey());
        order.setCreateTime(new Date());
        order.setStaffId(getUser().getUserId());
        order.setStaffName(getUser().getUsername());
        String appointType = order.getAppointType();
        if (AppointType.APPOINT.getKey().equals(appointType)) {
            order.setStatus(OrderStatus.NEW_ORDER.getKey());
        }
        /*if(order.getOrderTakerId()!=null&&order.getOrderTakerId() > 0){
            order.setAppointType(AppointType.APPOINT.getKey());
        }else{
            order.setAppointType(AppointType.INITIATIVE.getKey());
        }*/
        order.setAddress(areacodeService.getAddressByLastAdminCode(order.getAdminCode()));
        orderService.insert(order);
        return R.ok();
    }



    /**
     * 派单/发布
     */
    @PostMapping("/appoint")
    @RequiresPermissions("quickService:order:appoint")
    public R appoint(@RequestBody OrderEntity order){


        return R.ok();
    }





    /**
     * 完工审核
     */
    @PostMapping("/completeAudit")
    @RequiresPermissions("quickService:order:completeAudit")
    public R completeAudit(@RequestBody OrderEntity order){
        boolean flag =  orderService.completeAudit(order,getUser());
        if(!flag){
            return R.error("完工审核失败");
        }
        return R.ok();
    }



    /**
     *完成对账
     */
    @PostMapping("/accountChecking")
    @RequiresPermissions("quickService:order:accountChecking")
    public R accountChecking(@RequestBody OrderEntity order){
        boolean flag =  orderService.accountChecking(order,getUser());
        if(!flag){
            return R.error("对账失败");
        }
        return R.ok();
    }

    /**
     *  申请售后
     */
    @PostMapping("/afterSales")
    @RequiresPermissions("quickService:order:accountChecking")
    public R afterSales(@RequestBody OrderEntity order){
        boolean flag =  orderService.afterSales(order,getUser());
        if(!flag){
            return R.error("申请售后失败");
        }
        return R.ok();
    }



    /**
     * 修改
     */
    @PostMapping("/update")
    @RequiresPermissions("quickService:order:update")
    public R update(@RequestBody OrderEntity order){
        if (AppointType.INITIATIVE.getKey().equals(order.getAppointType())) {
            order.setOrderTakerId(null);
        }
        order.setAddress(areacodeService.getAddressByLastAdminCode(order.getAdminCode()));
        orderService.updateById(order);
        OrderOperationEntity operation = new OrderOperationEntity();
        operation.setOperatorName(getUser().getUsername());
        operation.setOperatorId(getUserId());
        operation.setOperationDesc("修改工单");
        operation.setOperationTime(new Date());
        operation.setOrderId(order.getId());
        orderOperationService.insert(operation);
        return R.ok();
    }

    /**
     * 删除
     */
    @PostMapping("/delete")
    @RequiresPermissions("quickService:order:delete")
    public R delete(@RequestBody Long[] ids){
        orderService.deleteBatchIds(Arrays.asList(ids));

        return R.ok();
    }
}
